﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using GONES.Tools.Common;
using GONES.IBLL;
using GONES.Model;

public partial class Console_System_UserRights_UserRightsTreeSet : CPage
{
    public string UserId { get; set; }
    string pcodestr = "";
    protected void Page_Load(object sender, EventArgs e)
    {
          UserId = Request.QueryString["id"].ToString();
    }

    public string GetNavMenuList(string ParentId)
    {
        StringBuilder treestr = new StringBuilder();
        IList<DataFilter> dataFilters = new List<DataFilter>();
        var navManager = AppContext.GetObject("S_NavManager") as IS_NavManager;
        DataFilter df = new DataFilter() { Type = "string", Value = ParentId, Field = "Parentid" };
        df.Comparison = "eq";
        dataFilters.Add(df);
        IList<S_Nav> navlist = navManager.List(dataFilters);
        S_Nav nav = new S_Nav();
        for (int i = 0; i < navlist.Count; i++)
        {
            nav = (S_Nav)navlist[i]; //target=\"dialog\" rel=\"{1}\"
            treestr.Append(string.Format("<li><a href=\"#\"  tname=\"{0}\" tvalue=\"{1}\">{2} </a>",
                                    "one", nav.ID,nav.Title));
            GetChildNavMenuList(nav.ID, ref treestr);
        }
        treestr.Append("</li>");

        return treestr.ToString();
    }

    public void GetChildNavMenuList(string parentId, ref StringBuilder treestr)
    {
        IList<DataFilter> dataFilters = new List<DataFilter>();
        var navManager = AppContext.GetObject("S_NavManager") as IS_NavManager;
        DataFilter df = new DataFilter() { Type = "string", Value = parentId, Field = "Parentid" };
        df.Comparison = "eq";
        dataFilters.Add(df);
        IList<S_Nav> navlist = navManager.List(dataFilters);
        S_Nav nav = new S_Nav();
        treestr.Append("<ul>");
        for (int i = 0; i < navlist.Count; i++)
        {
            nav = (S_Nav)navlist[i]; // target=\"dialog\" rel=\"{1}\"
            treestr.Append(string.Format("<li><a href=\"#\"  tname=\"{0}\" tvalue=\"{1}\">{2} </a>",
                                          "two", nav.ID, nav.Title));
            GetChildNavPermisionMenuList(nav.ID, ref treestr);
            GetChildNavMenuList(nav.ID, ref treestr);
        }
        treestr.Append("</ul>");
    }
   
    public void GetChildNavPermisionMenuList(string navId, ref StringBuilder treestr)
    {
        IList<DataFilter> dataFilters = new List<DataFilter>();
        var navpermissionManager = AppContext.GetObject("S_NavPermissionManager") as IS_NavPermissionManager;
        var userNavPermissionManager = AppContext.GetObject("S_UserNavpermissionManager") as IS_UserNavpermissionManager;
        var permissionsManager = AppContext.GetObject("S_PermissionsManager") as IS_PermissionsManager;
        IList<S_NavPermission> navpermisionlist = navpermissionManager.GetNavPermissionList(navId);
        S_NavPermission navpermission = new S_NavPermission();
        treestr.Append("<ul>");
        for (int i = 0; i < navpermisionlist.Count; i++)
        {
            navpermission = navpermisionlist[i];
            pcodestr = permissionsManager.Get(navpermission.PermisionId).Pname;
            if ((userNavPermissionManager.GetUserNavPermissionByUid(UserId).Where
                (u=>u.NavPermissionId==navpermission.ID).ToList().Count>0))
            {
                treestr.Append(string.Format("<li><a checked='checked'  href=\"#\"  tname=\"{0}\" tvalue=\"{1}\">{2} </a></li>",
                                           "three", navpermission.ID + "|", pcodestr));
            }
            else
            {
                treestr.Append(string.Format("<li><a href=\"#\"  tname=\"{0}\" tvalue=\"{1}\">{2} </a></li>",
                                                       "three", navpermission.ID + "|", pcodestr));
            }
        }
        treestr.Append("</ul>");
    }
}